/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proms.net.controle;

import java.security.*;

public class Seguranca {

    public static String encripta(String senha) {
        try {
            String retorno ="";
            MessageDigest digest = MessageDigest.getInstance("MD5");
            digest.update(senha.getBytes());
            //BASE64Encoder encoder = new BASE64Encoder();
            byte[] hash = digest.digest();
            for (int i = 0; i < hash.length; i++) {
                retorno += hash[i];
            }
            return retorno;
        } catch (NoSuchAlgorithmException ns) {
            ns.printStackTrace();
            return senha;
        }
    }

    public static String encripta(String senha, String algoritmo) {
        try {
            String retorno = null;
            MessageDigest digest = MessageDigest.getInstance(algoritmo);
            digest.update(senha.getBytes());
            //BASE64Encoder encoder = new BASE64Encoder();
            byte[] hash = digest.digest();
            for (int i = 0; i < hash.length; i++) {
                retorno += hash[i];
            }
            return retorno;
        } catch (NoSuchAlgorithmException ns) {
            ns.printStackTrace();
            return senha;
        }
    }

    public static boolean compara(String senha, String criptografada) {
        return criptografada.equals(encripta(senha));
    }

    public static boolean compara(String senha, String criptografada, String agoritmo) {
        return criptografada.equals(encripta(senha, agoritmo));
    }
}

